package action;

import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import bean.Tree;
import bean.Tt;
import bean.User;

import com.opensymphony.xwork2.ActionSupport;

public class ExtjsAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 412313925712733317L;
	
	private static Set<User> users=new HashSet<User>();
	private static Set<Tt> tt=new HashSet<Tt>();
	
	static{
//		User(String name,String email,int age,String sex,long deposit,String birthday,boolean isIt)
		User u1=new User("fm","xxx@xx.com",25,"男",200000,"10/02/1980",true);
		User u2=new User("fm","xxx@xx.com",25,"男",2400000,"10/02/1981",false);
		User u3=new User("张三","333@xx.com",25,"女",2003000,"10/03/1982",true);
		User u4=new User("李四","444@xx.com",25,"男",20456231,"10/04/1983",true);
		User u5=new User("王五","555@xx.com",25,"女",20000045,"10/05/1984",false);
		users.add(u1);
		users.add(u2);
		users.add(u3);
		users.add(u4);
		users.add(u5);
		//-----------------------------------
		Tt t1=new Tt("test",100);
		Tt t2=new Tt("test",200);
		tt.add(t1);
		tt.add(t2);
	}
	//grid实验数据-------------------------------------------------------------
	public void test() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		StringBuffer grid =new StringBuffer("{\"total\":\""+tt.size()+"\",\"topics\":");
		JSONArray jArray=JSONArray.fromObject(tt);
		grid.append(jArray.toString());
		grid.append("}");
		System.out.println(grid);
		writer.write(jArray.toString());
		writer.close();	
	}
	//-------------------------------------------------------------------------
	public void getJson() throws IOException
	{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		StringBuffer grid =new StringBuffer("{\"total\":\""+users.size()+"\",\"topics\":");
//		for(User user:users)
//		{
//			JSONObject jObject=JSONObject.fromObject(user);
//			grid.append(jObject.toString());
//		}
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日");
//		String result=sdf.format(new Date());
//		writer.write(result);
		JSONArray jArray=JSONArray.fromObject(users);
		grid.append(jArray.toString());
		grid.append("}");
		System.out.println(grid);
		writer.write(jArray.toString());
		writer.close();	
	}
	//-------------------------------------------------------------------------
	public void deleteData(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String ids=request.getParameter("ids");
		System.out.println(ids);
	}
	//-------------------------------------------------------------------------
	public void readDeptTree() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		StringBuffer grid =new StringBuffer("{\"id\":\"-1\",\"leaf\":false,\"text\":\"root\",children:[]}");
		System.out.println("dept tree load");
		writer.write(grid.toString());
		writer.close();	
	}
	//-------------------------------------------------------------------------
	public void updateList() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		HttpServletRequest request=ServletActionContext.getRequest();
		response.setCharacterEncoding("utf-8");
		System.out.println(request.getParameter("data"));
		Writer writer = response.getWriter();
		StringBuffer grid =new StringBuffer("{success:'update ok'}");
		System.out.println("list update ok");
		writer.write(grid.toString());
		writer.close();	
	}
	//data 4 Tree--------------------------------------------------------------------
	public void getTree() throws IOException
	{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		
		Tree t=new Tree();
			t.setId("01");
			t.setText("技术部");
			t.setLeaf(false);
		Tree t1=new Tree();
			t1.setId("0101");
			t1.setText("研发部");
			t1.setLeaf(true);
		Tree t2=new Tree();
			t2.setId("0102");
			t2.setText("研发部");
			t2.setLeaf(true);
		Set<Tree> set1=new HashSet<Tree>();
			set1.add(t1);
			set1.add(t2);
		t.setChildren(set1);
		
		Tree tt=new Tree();
			tt.setId("02");
			tt.setText("后勤部");
			tt.setLeaf(false);
		Tree tt1=new Tree();
			tt1.setId("0201");
			tt1.setText("人事部");
			tt1.setLeaf(true);
		Tree tt2=new Tree();
			tt2.setId("0202");
			tt2.setText("行政部");
			tt2.setLeaf(true);
		Set<Tree> set2=new HashSet<Tree>();
			set2.add(tt1);
			set2.add(tt2);
		tt.setChildren(set2);
		
		Tree root =new Tree();
			root.setText("root");
			root.setId("0");
		Set<Tree> r=new HashSet<Tree>();
			r.add(t);
			r.add(tt);
		root.setChildren(r);
		
		JSONArray jArray=JSONArray.fromObject(root);
		String treeJson=jArray.toString();//直接转成json对象后多了一对中括号，所以使用substring
		System.out.println(treeJson.substring(1, treeJson.length()-1));
		writer.write(treeJson.substring(1, treeJson.length()-1));
		writer.close();	
	}
}
